<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="ID">
            <a-input v-decorator="['search']" placeholder="아이디 입력" />
          </a-form-item>
          <a-form-item label="레퍼럴">
            <a-input v-decorator="['search_refer']" placeholder="추천인 ID를 입력하세요." />
          </a-form-item>
          <!-- <a-form-item label="会员等级">
            <a-select v-decorator="['gradeId', { initialValue: 0 }]">
              <a-select-option :value="0">全部</a-select-option>
              <a-select-option
                v-for="(item, index) in gradeList"
                :key="index"
                :value="item.grade_id"
              >{{ item.name }}</a-select-option>
            </a-select>
          </a-form-item> -->
          <a-form-item label="등록일자">
            <a-range-picker format="YYYY-MM-DD" v-decorator="['betweenTime']" :placeholder="['시작일', '종료일']" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit" @click="searchBtn">검색</a-button>
          </a-form-item>
        </a-form>
      </a-row>
    </div>




    <s-table
      ref="table"
      rowKey="user_id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
      :scroll="{ x: 1450 }"
    >
      <!-- 实际消费金额 -->
      <span slot="is_master" slot-scope="text">
        <span class="c-p">{{ text == 0 ? 'NO' : 'YES' }}</span>
      </span>
      <!-- 操作 -->
      <span class="actions" slot="action" slot-scope="item">
        <a title="修改邮件" v-action:recharge @click="handleEmail(item)">메일수정</a>
        <a title="会员充值" v-action:recharge @click="handleRecharge(item)">상품구매</a>
        <a v-action:delete @click="handleDelete(item)">서브계정생성</a>
      </span>
    </s-table>
    <GradeForm ref="GradeForm" :gradeList="gradeList" @handleSubmit="handleRefresh" />
    <RechargeForm ref="RechargeForm" @handleSubmit="handleRefresh" />
    <EmailForm ref="EamilForm" @handleSubmit="handleRefresh" :oldEmail="oldEmail"></EmailForm>
  </a-card>
</template>

<script>
import * as Api from '@/api/user'
import * as GradeApi from '@/api/user/grade'
import { STable } from '@/components'
import { GradeForm, RechargeForm, EmailForm } from './modules'
import PlatformIcon from '@/components/PlatformIcon'

export default {
  name: 'Index',
  components: {
    STable,
    GradeForm,
    RechargeForm,
    PlatformIcon,
    EmailForm
  },
  data() {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '아이디',
          dataIndex: 'user_no',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '계정',
          dataIndex: 'account_id',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '회원 이름',
          dataIndex: 'real_name',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '전화번호',
          dataIndex: 'phone',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '이메일',
          dataIndex: 'email',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: 'PDT잔액',
          dataIndex: 'pdt',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '실소비총액',
          dataIndex: 'buy_total',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '업적',
          dataIndex: 'master_price',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: 'Master여부',
          dataIndex: 'is_master',
          width: '180px',
          scopedSlots: { customRender: 'is_master' },
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '추천인',
          dataIndex: 'refer_no',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '메인계정',
          dataIndex: 'parent.account_id',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '등록일자',
          dataIndex: 'create_time',
          width: '180px',
          // 单元格超出换行
          customCell: () => {
            return {
              style: {
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                whiteSpace: 'normal',
                minHeight: '50px',
                width: '50px',
              },
            }
          },
        },
        {
          title: '작업',
          // dataIndex: 'action',
          width: '220px',
          scopedSlots: { customRender: 'action' },
          fixed: 'right',
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        return Api.list({ ...param, ...this.queryParam }).then((response) => response.data.list)
      },
      // 会员等级列表
      gradeList: [],
      // 旧邮箱
      oldEmail:'',
    }
  },
  created() {
    // console.log(this.$route.query.time)
    setTimeout(() => {
      this.searchForm.setFieldsValue({
        betweenTime: this.$route.query.time,
      })
    }, 1000)
    this.queryParam.betweenTime = this.$route.query.time

    // 获取会员等级列表
    // this.getGradeList()
  },
  methods: {
    searchBtn() {
      // console.log(this.searchForm, 11111111)
      this.searchForm.validateFields((err, values) => {
        // 这里做逻辑处理
        console.log(values, 11111111) // { username: '' }
      })
    },

    // 获取会员等级列表
    getGradeList() {
      GradeApi.all().then((result) => {
        this.gradeList = result.data.list
      })
    },

    // 修改会员等级
    handleGrade(item) {
      this.$refs.GradeForm.handle(item)
    },

    // 会员充值
    handleRecharge(item) {
      this.$refs.RechargeForm.handle(item)
    },

    // 修改邮箱
    handleEmail(item){
      this.$refs.EamilForm.handle(item)
      console.log(item,'当前项')
      this.oldEmail = item.email
    },

    /**
     * 删除记录
     */
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '현재 계정의 하위 계정을 만드시겠습니까?',
        content: '생성 후 복구 불가능',
        cancelText:"Cancel",
        okText:"OK",
        onOk() {
          return Api.created({ user_id: item.user_id })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => modal.destroy())
        },
      })
    },

    // 确认搜索
    handleSearch(e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },
  },
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
